<?php
/**
 * Minhlog 数据库验证类
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Minh
 * @package    Minh_Auth
 * @copyright  Copyright (c) 2009-2010 Minh.Shu Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: Db.php 2 2010-01-17 13:49:08Z Minh.Shu $
 *
 */

class Minh_Auth_Db
{
    public function __construct ()
    {
        $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
        $resource = $bootstrap->getPluginResource('db');
        $db = $resource->getDbAdapter();

        $this->_auth = Zend_Auth::getInstance();
        $this->_auth->setStorage(new Zend_Auth_Storage_Session('Minhlog'));
        $this->_storage = $this->_auth->getStorage();

        $this->_authAdapter = new Zend_Auth_Adapter_DbTable($db);
        $this->_authAdapter->setTableName('mlog_user')->setIdentityColumn('username')->setCredentialColumn('password');
    }

    /**
     * 验证用户信息
     * @param string $username
     * @param string $password
     * @return object
     */
    public function authUser ($username, $password)
    {
        $this->_authAdapter->setIdentity($username);
        $this->_authAdapter->setCredential($password);
        $authResult = $this->_auth->authenticate($this->_authAdapter);
        return $authResult;
    }

    /**
     * 认证持久化
     * @return
     */
    public function authPersist ()
    {
        return $this->_storage->write($this->_authAdapter->getResultRowObject(null, 'password'));
    }

    /**
     * 获取验证信息
     * @return
     */
    public function getAuthInfo ()
    {
        return $this->_storage->read();
    }
}
